package com.sky.controller.admin;

import com.sky.dto.SetmealDTO;
import com.sky.dto.SetmealPageQueryDTO;
import com.sky.result.PageResult;
import com.sky.result.Result;
import com.sky.service.SetmealService;
import com.sky.vo.SetmealVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@Api(tags = "套餐相关的接口")
@RequestMapping("/admin/setmeal")
@Slf4j
public class SetmealController {

    @Autowired
    private SetmealService setmealService;
    /*
    * 新增套餐及其菜品
    * */
    @PostMapping
    @CacheEvict(cacheNames = "setmealCache",key = "#setmealDTO.categoryId")
    public Result save(@RequestBody SetmealDTO setmealDTO){
        log.info("新增套餐:{}",setmealDTO);
        setmealService.save(setmealDTO);
        return Result.success();
    }

    /*
    * 分页查询套餐
    * */
    @ApiOperation("分页查询套餐")
    @GetMapping("/page")
    public Result<PageResult> page(SetmealPageQueryDTO setmealPageQueryDTO){
        log.info("分页查询套餐:{}",setmealPageQueryDTO);
        PageResult page = setmealService.page(setmealPageQueryDTO);
        return Result.success(page);
    }

    /*
    * 根据id查询套餐及其菜品关系
    * */
    @ApiOperation("根据id查询套餐及其菜品关系")
    @GetMapping("/{id}")
    public Result<SetmealVO> getById(@PathVariable Long id){
        log.info("套餐id：{}，查询套餐及其菜品信息",id);
        SetmealVO setmealVO = setmealService.queryById(id);
        return Result.success(setmealVO);
    }

    /*
    * 修改套餐及其菜品关系
    * */
    @ApiOperation("修改套餐及其菜品关系")
    @PutMapping
    @CacheEvict(cacheNames = "setmealCache",key = "#setmealDTO.categoryId")
    public Result update(@RequestBody SetmealDTO setmealDTO) {
        log.info("修改套餐及其菜品关系:{}", setmealDTO);
        setmealService.update(setmealDTO);
        return Result.success();
    }

    /*
    * 起售停售套餐
    * */
    @ApiOperation("起售停售套餐")
    @PostMapping("/status/{status}")
    @CacheEvict(allEntries = true)
    public Result enableOrDisable(@PathVariable Integer status,@RequestParam Long id){
        log.info(status == 1 ? "起售套餐id:{}" : "停售套餐id:{}",id);
        setmealService.enableOrDisable(id,status);
        return Result.success();
    }

    /*
    * 批量删除套餐
    * */
    @ApiOperation("批量删除套餐")
    @DeleteMapping
    @CacheEvict(allEntries = true)
    public Result deleteBatch(@RequestParam List<Long> ids){
        log.info("批量删除套餐ids:{}",ids);
        setmealService.deleteBatch(ids);
        return Result.success();
    }

}
